package com.quanyan.vote.utils;

import com.quanyan.common.utils.DateUtils;
import com.quanyan.vote.constans.VoteConstans;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2016/7/22.
 */
public class VoteUtil {

    /**
     * 判断当前时间是否在指定时间内
     * @param start
     * @param end
     * @param date
     * @return
     */
    public static int compareDate(Date start, Date end, Date date) {
        if (date.getTime() < start.getTime()) {//未到投票时间
            return VoteConstans.VOTE_TIME_IN;
        }else if(date.getTime() > end.getTime()){//投票活动已结束
            return VoteConstans.VOTE_TIME_OUT;
        }else{//现在可以投票
            return VoteConstans.VOTE_TIME;
        }
    }

    /**
     * 判断指定时间是不是在今天凌晨后
     * @param date
     * @return
     */
    public static boolean compareDate(Date date){
        if(date.getTime()>=getTodayMorningDate().getTime()){
            return true;
        }
        return false;
    }

    /**
     * 得到今天凌晨时间
     * @return
     */
    public static Date getTodayMorningDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 得到明天凌晨时间
     * @return
     */
    public static Date getNextMorningDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 得到昨天凌晨时间
     * @return
     */
    public static Date getYesterdayMorningDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, (calendar.get(Calendar.DAY_OF_MONTH) - 1));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 判断是否为double类型数字
     * @param value
     * @return
     */
    public static boolean checkNumber(double value){
        String str = String.valueOf(value);
        String regex = "^(-?[1-9]\\d*\\.?\\d*)|(-?0\\.\\d*[1-9])|(-?[0])|(-?[0]\\.\\d*)$";
        return str.matches(regex);
    }

    public static boolean checkNumber(int value){
        String str = String.valueOf(value);
        String regex = "^(-?[1-9]\\d*\\.?\\d*)|(-?0\\.\\d*[1-9])|(-?[0])|(-?[0]\\.\\d*)$";
        return str.matches(regex);
    }

    public static boolean checkNumber(String value){
        String regex = "^(-?[1-9]\\d*\\.?\\d*)|(-?0\\.\\d*[1-9])|(-?[0])|(-?[0]\\.\\d*)$";
        return value.matches(regex);
    }

    public static void main(String[] args) {
        System.out.println(DateUtils.DAY_DATE_FORMAT_HH_MM_SS.format(VoteUtil.getTodayMorningDate())+"------"+DateUtils.DAY_DATE_FORMAT_HH_MM_SS.format(VoteUtil.getNextMorningDay()));
    }

}
